顯示視訊串流基本要素為使用cv2.VideoCapture()打開視訊源,然後在視訊源打開的狀況下以frame讀出並用cv2.imshow()一張張的播放出來,以下為例,
def init_notebookpage_3(self):
self.notebookpage_3_tab = tk.Frame(self.notebook2 )
self.notebook2.add(self.notebookpage_3_tab, text="notebookpage_3")
在面板上我做一個tk.Button按鍵,title為'StreamOpenCV',command設為self.StreamOpenCV,按下就可執行功能。
StreamOpenCVbutton = tk.Button(self.notebookpage_3_tab, font=('Courier', 7), text = "StreamOpenCV", command = self.StreamOpenCV)
StreamOpenCVbutton.pack(side = tk.TOP, expand=tk.YES, fill=tk.BOTH)
定義一個播放stream的函式StreamOpenCV,指定self.cap的視訊來源為cv2.VideoCapture(0)
def StreamOpenCV(self, event = None):
self.cap = cv2.VideoCapture(0)
當self.cap.isOpened()的前提下,以frame儲存self.cap.read()來的資料,cv2.imshow播放frame
while(self.cap.isOpened()):
ret, frame = self.cap.read()
cv2.imshow("StreamOpenCV", frame)
if c == 27: break表示等待按'Esc'跳出;
c = cv2.waitKey(1)
if c == 27: break
self.cap.release()
cv2.destroyAllWindows()